package ReflectDemo;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: lenovo
 * Date: 2022-09-20
 * Time: 15:03
 */
// 创建对象
public class reflectdemo {
    public static  void reflectnewInstance() {
        //在类外获取反射对象
        Class<?> c1 = null;
        try {
            c1 = Class.forName("ReflectDemo.Test$Student");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        //调用反射对象里newInstance方法,实例化一个对象
        Test.Student student = null;
        try {
            student = (Test.Student)c1.newInstance();//调用的是不带参数的构造方法
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        System.out.println(student);
    }

    // 反射私有的构造方法 屏蔽内容为获得公有的构造方法
    public static void reflectPrivateConstructor() {
        try {
            Class<?> c1= Class.forName("ReflectDemo.Test$Student");
            Constructor<?> constructor =  c1.getDeclaredConstructor(String.class,int.class);
            constructor.setAccessible(true);
            Test.Student student = (Test.Student) constructor.newInstance("张三",16);
            System.out.println(student);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

    }

    // 反射私有属性
    public static void reflectPrivateField(){
        try {
           Class<?> c =  Class.forName("ReflectDemo.Test$Student");
           //实例化对象
            Test.Student student = (Test.Student)c.newInstance();
           Field field = c.getDeclaredField("name");
           field.setAccessible(true);
           field.set(student,"美羊羊");
            System.out.println(student);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    // 反射私有方法
    public static void reflectPrivateMethod(){
        try {
            //获取到反射对象并且实例化
           Class<?> c =  Class.forName("ReflectDemo.Test$Student");
            Test.Student student = (Test.Student)c.newInstance();
            //通过对象获取到这个私有方法
            Method method = c.getDeclaredMethod("function",String.class);
            method.setAccessible(true);
            method.invoke(student,"这是我通过反射传给你的");


        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        //reflectnewInstance();
        //reflectPrivateConstructor();
        //reflectPrivateField();
        reflectPrivateMethod();
    }
}
